package com.ruoyi.common.utils;

import java.io.UnsupportedEncodingException;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringRandomUtils {

    public static String createRandom() {
        int length = 5;

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            char c = generateRandomChar();
            sb.append(c);
        }

        String randomText = sb.toString();
        System.out.println("随机生成的文本：" + randomText);
        return randomText;
    }

    /**
     * 获取指定长度随机简体中文
     *
     * @param len int
     * @return String
     */

    public static String getRandomJianHan(int len) {

        String ret = "";

        for (int i = 0; i <= len; i++) {

            String str = null;

            int hightPos, lowPos; // 定义高低位

            Random random = new Random();

            hightPos = (176 + Math.abs(random.nextInt(39))); //获取高位值

            lowPos = (161 + Math.abs(random.nextInt(93))); //获取低位值

            byte[] b = new byte[2];

            b[0] = (new Integer(hightPos).byteValue());

            b[1] = (new Integer(lowPos).byteValue());

            try {

                str = new String(b, "GBk"); //转成中文

            } catch (UnsupportedEncodingException ex) {

                ex.printStackTrace();

            }

            ret += str;

        }

        return ret;

    }


    public static void main(String[] args) {
        System.out.println(getRandomJianHan(10));
    }
    public static char generateRandomChar() {
        // 设置要生成的字符集合（这里只包含了大写字母）
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        // 获取字符集合的长度
        int size = characters.length();

        // 生成一个在[0,size-1]范围内的随机索引
        int index = Math.abs((new Random()).nextInt()) % size;

        return characters.charAt(index);
    }



}
